package com.weige.order.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.weige.api.client.UserClient;
import com.weige.api.user.User;
import com.weige.order.mapper.OrderMapper;
import com.weige.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired(required = false)
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2. 组装URL
        //String url = "http://localhost:8081/user/" + order.getUserId();
        //String url = "http://userservice/user/" + order.getUserId();
        // 3. 发起调用
        //User user = restTemplate.getForObject(url, User.class);
        //  利用Feign发起HTTP请求
        User user = userClient.findById(order.getUserId());
        order.setUser(user);
        // 4.返回
        return order;
    }

    @SentinelResource("goods")
    public void queryGoods() {
        System.err.println("查询商品");
    }
}
